<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * 信用卡管理
 */
class Credit extends Admin_Controller {
	
    function __construct()
    {
        parent::__construct();
        $this->load->model('credit_model');
        
        $this->settings->load('credit');
        $this->category = setting("credit");
    }
    //信用卡列表
    public function index()
    {
        $where = array();
        if(isset($_REQUEST['key']) && trim($_REQUEST['key'])){
            $keyword = trim($_REQUEST['key']);
            $where['credit.title'] = $keyword;
        }
        $getcount = $this->credit_model->get_credit_count($where);
        $navigation_position = 'outside';
        $records_per_page = 10;
        $this->xpage->navigation_position(isset($navigation_position) && in_array($navigation_position, array('left', 'right')) ? $navigation_position : 'outside');
        $this->xpage->records($getcount);
        $this->xpage->records_per_page($records_per_page);
        $this->xpage->labels("<<", ">>");
        $list = $this->credit_model->get_credit_list($where, (($this->xpage->get_page() - 1) * $records_per_page) ,$records_per_page);
        foreach ($list as $key=>$val){
            $list[$key]['create_time'] = date("Y-m-d",$val['create_time']);
        }
        $this->template->assign('list',$list);
        $this->template->assign('Total',$getcount);
        $this->template->assign('keyname',$keyword);
        $this->template->assign('showpage',$this->xpage->render(true));
        $this->template->assign('segment_p','index');
        $this->template->load('credit/index');
    }
    
    public function credit_add(){
        if($_POST){
            $data = array();
            $data['title'] = trim($_POST['title']);
            $data['logo'] = trim($_POST['logo']);
            $data['link'] = trim($_POST['link']);
            $data['reviews'] = trim($_POST['reviews']);
            $data['activity_one'] = trim($_POST['activity_one']);
            $data['activity_two'] = trim($_POST['activity_two']);
            $data['activity_three'] = trim($_POST['activity_three']);
            $data['bank_type'] = trim($_POST['bank_type']);
            $data['use_type'] = trim($_POST['use_type']);
            $data['degree_type'] = trim($_POST['degree_type']);
            $data['coin_type'] = trim($_POST['coin_type']);
            $data['organ_type'] = trim($_POST['organ_type']);
            $data['interest_des'] = trim($_POST['interest_des']);
            $data['year_fee'] = trim($_POST['year_fee']);
            $data['withdraw_amount'] = trim($_POST['withdraw_amount']);
            $data['baseinfo'] = trim($_POST['baseinfo']);
            $data['about_fee'] = trim($_POST['about_fee']);
            $data['privilege'] = trim($_POST['privilege']);
            $data['sort'] = trim($_POST['sort']);
            $data['is_hide'] = trim($_POST['is_hide']);
            $data['create_time'] = time();
            
            $result = $this->credit_model->insert_credit($data);
            if($result){
                $mssage['info']	  = '添加成功';
                $mssage['status'] = 1;
                $mssage['url']	  = site_url("credit/index");
                echo json_encode($mssage);exit;
            }else{
                $mssage['info']	  = '添加失败';
                $mssage['status'] = 0;
                $mssage['url']	  = site_url("credit/index");
                echo json_encode($mssage);exit;
            }
        }
        $bank_type = $this->category['bank_type']['children'];
        $use_type = $this->category['use_type']['children'];
        $degree_type = $this->category['degree_type']['children'];
        $coin_type = $this->category['coin_type']['children'];
        $organ_type = $this->category['organ_type']['children'];
        $this->template->assign('bank_type',$bank_type);
        $this->template->assign('use_type',$use_type);
        $this->template->assign('degree_type',$degree_type);
        $this->template->assign('coin_type',$coin_type);
        $this->template->assign('organ_type',$organ_type);
        
        $this->template->assign('segment_p','index');
        $this->template->load('credit/credit_add');
    }
    
    public function credit_edit(){
        if($_POST){
            $id = trim($_POST['id']);
            $data = array();
            $data['title'] = trim($_POST['title']);
            $data['logo'] = trim($_POST['logo']);
            $data['link'] = trim($_POST['link']);
            $data['reviews'] = trim($_POST['reviews']);
            $data['activity_one'] = trim($_POST['activity_one']);
            $data['activity_two'] = trim($_POST['activity_two']);
            $data['activity_three'] = trim($_POST['activity_three']);
            $data['bank_type'] = trim($_POST['bank_type']);
            $data['use_type'] = trim($_POST['use_type']);
            $data['degree_type'] = trim($_POST['degree_type']);
            $data['coin_type'] = trim($_POST['coin_type']);
            $data['organ_type'] = trim($_POST['organ_type']);
            $data['interest_des'] = trim($_POST['interest_des']);
            $data['year_fee'] = trim($_POST['year_fee']);
            $data['withdraw_amount'] = trim($_POST['withdraw_amount']);
            $data['baseinfo'] = trim($_POST['baseinfo']);
            $data['about_fee'] = trim($_POST['about_fee']);
            $data['privilege'] = trim($_POST['privilege']);
            $data['sort'] = trim($_POST['sort']);
            $data['is_hide'] = trim($_POST['is_hide']);
            
            $result = $this->credit_model->update_credit($data,$id);
            if($result){
                $mssage['info']	  = '更新成功';
                $mssage['status'] = 1;
                $mssage['url']	  = site_url("credit/index");
                echo json_encode($mssage);exit;
            }else{
                $mssage['info']	  = '更新失败';
                $mssage['status'] = 0;
                $mssage['url']	  = site_url("credit/index");
                echo json_encode($mssage);exit;
            }
        }
        $bank_type = $this->category['bank_type']['children'];
        $use_type = $this->category['use_type']['children'];
        $degree_type = $this->category['degree_type']['children'];
        $coin_type = $this->category['coin_type']['children'];
        $organ_type = $this->category['organ_type']['children'];
        
        $id = isset($_GET['id']) ? trim($_GET['id']) : 0;
        $item = $this->credit_model->get(array("id"=>$id));
        if(!$item){
            redirect("credit/index");
        }
        
        $this->template->assign('bank_type',$bank_type);
        $this->template->assign('use_type',$use_type);
        $this->template->assign('degree_type',$degree_type);
        $this->template->assign('coin_type',$coin_type);
        $this->template->assign('organ_type',$organ_type);
        $this->template->assign('organ_type',$organ_type);
        $this->template->assign('item',$item);
        $this->template->assign('segment_p','index');
        $this->template->load('credit/credit_edit');
    }
    function sethide(){
        if($_GET){
            $mssage = array();
            $id	   = trim($_GET['id']);
            $is_hide = trim($_GET['is_hide']);
            $data = array();
            $data['is_hide'] = $is_hide;
            $result = $this->credit_model->update_credit($data,$id);
            if($result){
//                update_cache("credit");
                if($is_hide==1){
                    $mssage['info']	  = '显示成功';
                }else{
                    $mssage['info']	  = '隐藏成功';
                }
                $mssage['status'] = 1;
                $mssage['url']	  = $_SERVER['HTTP_REFERER'];
                echo json_encode($mssage);exit;
            }else{
                $mssage['info']	  = '操作失败';
                $mssage['status'] = 0;
                $mssage['url']	  = $_SERVER['HTTP_REFERER'];
                echo json_encode($mssage);exit;
            }

        }
    }
    //信用卡类别
    public function category(){
        $where = array();
        $where['pid'] = 0;
        $list = $this->credit_model->get_credit_category_list($where,0,10);
        $this->template->assign('list',$list);
        $this->template->load('credit/category');
    }
    
    //添加信用卡分类
    public function category_add(){
        if($_POST){
            $data = array();
            $data['title'] = trim($_POST['title']);
            $data['code'] = trim($_POST['code']);
            $data['sort'] = trim($_POST['sort']);
            $data['url'] = trim($_POST['url']);
            $data['desc'] = trim($_POST['desc']);
            $data['is_hide'] = trim($_POST['is_hide']);
            $data['groups'] = $data['code'];
            $data['pid'] = 0;
            $data['level'] = 0;
            if(!$data['title']){
                $mssage['info']	  = '类别名称不能为空';
                $mssage['status'] = 0;
                $mssage['url']	  = "";
                echo json_encode($mssage);exit;
            }
            
            if(!$data['code']){
                $mssage['info']	  = '类别标识不能为空';
                $mssage['status'] = 0;
                $mssage['url']	  = "";
                echo json_encode($mssage);exit;
            }
            
            $temp = $this->credit_model->get_credit_category_list(array("code"=>$data['code']),0,10);
            if($temp){
                $mssage['info']	  = '类别标识不能重复';
                $mssage['status'] = 0;
                $mssage['url']	  = "";
                echo json_encode($mssage);exit;
            }
            
            $result = $this->credit_model->insert_credit_category($data);
            if($result){
                $mssage['info']	  = '添加成功';
                $mssage['status'] = 1;
                $mssage['url']	  = site_url('credit/category');
                echo json_encode($mssage);exit;
            }else{
                $mssage['info']	  = '添加失败';
                $mssage['status'] = 0;
                $mssage['url']	  = site_url('credit/category');
                echo json_encode($mssage);exit;
            }
            
        }
        $this->template->assign('segment_p','category');
        $this->template->load('credit/category_add');
    }
    //修改信用卡分类
    public function category_edit(){
        if($_POST){
            $id = trim($_POST['id']);
            $data = array();
            $data['title'] = trim($_POST['title']);
            $data['url'] = trim($_POST['url']);
            $data['desc'] = trim($_POST['desc']);
            $data['sort'] = trim($_POST['sort']);
            $data['is_hide'] = trim($_POST['is_hide']);
            if(!$data['title']){
                $mssage['info']	  = '类别名称不能为空';
                $mssage['status'] = 0;
                $mssage['url']	  = "";
                echo json_encode($mssage);exit;
            }
            
            $result = $this->credit_model->update_category($data,$id);
            if($result){
                $mssage['info']	  = '修改成功';
                $mssage['status'] = 1;
                $mssage['url']	  = site_url('credit/category');
                echo json_encode($mssage);exit;
            }else{
                $mssage['info']	  = '修改失败';
                $mssage['status'] = 0;
                $mssage['url']	  = site_url('credit/category');
                echo json_encode($mssage);exit;
            }
            
        }
        $id = isset($_GET['id']) ? trim($_GET['id']) : 0;
        $item = $this->credit_model->get_credit_category(array("id"=>$id));
        if(!$item){redirect("credit/category");}
        $this->template->assign('item',$item);
        $this->template->assign('segment_p','category');
        $this->template->load('credit/category_edit');
    }
    //子分类列表
    function category_son(){
        $pid = trim($_GET['pid']);
        if(!$pid){
            redirect("credit/category");
        }
        $where = array();
        $where['pid'] = $pid;
        $getcount = $this->credit_model->get_credit_category_count($where);
        $navigation_position = 'outside';
        $records_per_page = 10;
        $this->xpage->navigation_position(isset($navigation_position) && in_array($navigation_position, array('left', 'right')) ? $navigation_position : 'outside');
        //$this->xpage->reverse(true);
        $this->xpage->records($getcount);
        $this->xpage->records_per_page($records_per_page);
        $this->xpage->labels("<<", ">>");
        $list = $this->credit_model->get_credit_category_list($where, (($this->xpage->get_page() - 1) * $records_per_page) ,$records_per_page);
        
        $item = $this->credit_model->get_credit_category(array("id"=>$pid));
        $this->template->assign('item',$item);
        $this->template->assign('list',$list);
        $this->template->assign('Total',$getcount);
        $this->template->assign('showpage',$this->xpage->render(true));
        $this->template->assign('segment_p','category');
        $this->template->load('credit/category_son');
    }
    //添加子分类
    function category_son_add(){
        if($_POST){
            $data = array();
            $data['title'] = trim($_POST['title']);
            $data['code'] = "";
            $data['url'] = trim($_POST['url']);
            $data['desc'] = trim($_POST['desc']);
            $data['groups'] = trim($_POST['groups']);
            $data['pid'] = trim($_POST['pid']);
            $data['is_hide'] = trim($_POST['is_hide']);
            $data['sort'] = trim($_POST['sort']);
            $data['level'] = trim($_POST['level']) + 1;
            if(!$data['title']){
                $mssage['info']	  = '类别名称不能为空';
                $mssage['status'] = 0;
                $mssage['url']	  = "";
                echo json_encode($mssage);exit;
            }
            
            $result = $this->credit_model->insert_credit_category($data);
            if($result){
                update_cache("credit");
                
                $mssage['info']	  = '添加成功';
                $mssage['status'] = 1;
                $mssage['url']	  = site_url('credit/category_son')."?pid=".$data['pid'];
                echo json_encode($mssage);exit;
            }else{
                $mssage['info']	  = '添加失败';
                $mssage['status'] = 0;
                $mssage['url']	  = site_url('credit/category_son')."?pid=".$data['pid'];
                echo json_encode($mssage);exit;
            }
            
        }
        $pid = isset($_GET['pid']) ? trim($_GET['pid']) : 0;
        $item = $this->credit_model->get_credit_category(array("id"=>$pid));
        if(!$item){
            redirect("credit/category");
        }
        $this->template->assign('item',$item);
        $this->template->assign('segment_p','category');
        $this->template->load('credit/category_son_add');
    }
    //修改子分类信息
    function category_son_edit(){
        if($_POST){
            $data = array();
            $id = trim($_POST['id']);
            $pid = trim($_POST["pid"]);
            $data['url'] = trim($_POST['url']);
            $data['desc'] = trim($_POST['desc']);
            $data['title'] = trim($_POST['title']);
            $data['is_hide'] = trim($_POST['is_hide']);
            $data['sort'] = trim($_POST['sort']);
            if(!$data['title']){
                $mssage['info']	  = '类别名称不能为空';
                $mssage['status'] = 0;
                $mssage['url']	  = "";
                echo json_encode($mssage);exit;
            }
            
            $result = $this->credit_model->update_category($data,$id);
            if($result){
                update_cache("credit");
                $mssage['info']	  = '添加成功';
                $mssage['status'] = 1;
                $mssage['url']	  = site_url('credit/category_son')."?pid=".$pid;
                echo json_encode($mssage);exit;
            }else{
                $mssage['info']	  = '添加失败';
                $mssage['status'] = 0;
                $mssage['url']	  = site_url('credit/category_son')."?pid=".$pid;
                echo json_encode($mssage);exit;
            }
            
        }
        $id = isset($_GET['id']) ? trim($_GET['id']) : 0;
        $item = $this->credit_model->get_credit_category(array("id"=>$id));
        if(!$item){
            $url = site_url("credit/category_son")."?pid=".$_GET['pid'];
            redirect($url);
        }
        $this->template->assign('item',$item);
        $this->template->assign('segment_p','category');
        $this->template->load('credit/category_son_edit');
    }
    
    
    //删除子分类
    function category_son_del(){
        if($_GET){
            $mssage = array();
            $category_id	   = trim($_GET['id']);
            $data = array();
            $result = $this->credit_model->delete_category($category_id);
            if($result){
                update_cache("credit");
                $mssage['info']	  = '删除成功';
                $mssage['status'] = 1;
                $mssage['url']	  = $_SERVER['HTTP_REFERER'];
                echo json_encode($mssage);exit;
            }else{
                $mssage['info']	  = '操作失败';
                $mssage['status'] = 0;
                $mssage['url']	  = $_SERVER['HTTP_REFERER'];
                echo json_encode($mssage);exit;
            }

        }
    }
    
    function setcategory_hide(){
        if($_GET){
            $mssage = array();
            $category_id	   = trim($_GET['id']);
            $is_hide = trim($_GET['is_hide']);
            $data = array();
            $data['is_hide'] = $is_hide;
            $result = $this->credit_model->update_category($data,$category_id);
            if($result){
                update_cache("credit");
                if($is_hide==1){
                    $mssage['info']	  = '显示成功';
                }else{
                    $mssage['info']	  = '隐藏成功';
                }
                $mssage['status'] = 1;
                $mssage['url']	  = $_SERVER['HTTP_REFERER'];
                echo json_encode($mssage);exit;
            }else{
                $mssage['info']	  = '操作失败';
                $mssage['status'] = 0;
                $mssage['url']	  = $_SERVER['HTTP_REFERER'];
                echo json_encode($mssage);exit;
            }

        }
    }
}
